package com.ABdolphin.forum.services.impl;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.dao.ArticleReplyMapper;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Article;
import com.ABdolphin.forum.model.ArticleReply;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IArticleReplyService;
import com.ABdolphin.forum.services.IArticleService;
import com.ABdolphin.forum.services.IUserService;
import com.ABdolphin.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource(type = ArticleServiceImpl.class)
    private IArticleService articleService;

    @Resource(type = UserServiceImpl.class)
    private IUserService userService;


    @Override
    public void create(ArticleReply articleReply) {
        if (articleReply == null ||
                articleReply.getPostUserId() == null ||
                articleReply.getArticleId() == null ||
                StringUtil.isEmpty(articleReply.getContent())) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //文章是否存在
        Article article = articleService.selectById(articleReply.getArticleId());
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //文章状态是否正常
        if (article.getState()==1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED));
        }
        //发送者是否存在
        User poster = userService.selectById(articleReply.getPostUserId());
        if (poster==null||poster.getDeleteState()==1){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //发送者是否禁言
        if (poster.getState()==1){
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_BANNED));
        }
        //创建对象,并且初始化
        ArticleReply insertReply = new ArticleReply();
        insertReply.setArticleId(article.getId());
        insertReply.setPostUserId(poster.getId());
        insertReply.setContent(articleReply.getContent());
        if (articleReply.getReplyId()!=null&&articleReply.getReplyUserId()!=null){
            insertReply.setReplyId(articleReply.getReplyId());
            insertReply.setReplyUserId(articleReply.getReplyUserId());
        }
        insertReply.setLikeCount(0);
        insertReply.setState((byte)0);
        insertReply.setDeleteState((byte)0);
        insertReply.setUpdateTime(new Date());
        insertReply.setCreateTime(new Date());
        //添加评论
        int result = articleReplyMapper.insertSelective(insertReply);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString()+", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
        //更新文章评论数+1
        articleService.addOneArticleReplyCountById(articleReply.getArticleId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //参数校验
        if (articleId==null||articleId<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //文章是否存在
        Article article = articleService.selectById(articleId);
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        return articleReplyMapper.selectByArticleId(articleId);
    }

    @Override
    public void addOneReplyLikeCountById(Long id) {
        //参数校验
        if (id==null||id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(id);
        if (articleReply==null||articleReply.getDeleteState()==1){
            log.info(ResultCode.FAILED_REPLY_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_REPLY_NOT_EXISTS));
        }
        ArticleReply updateArticleReply = new ArticleReply();
        updateArticleReply.setId(id);
        updateArticleReply.setLikeCount(articleReply.getLikeCount()+1);
        int result = articleReplyMapper.updateByPrimaryKeySelective(updateArticleReply);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }
}
